<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0%;
        padding: 0%;
        box-sizing: border-box;
      }
      #auto {
        width: 400px;
        height: 500px;
        background-color: aquamarine;
        margin: 50px auto;
        display: flex;
        flex-direction: column;
        align-items: center;
      }
      p {
        width: 100%;
        height: 50px;
        line-height: 50px;
        text-align: center;
        background-color: aqua;
      }
      div {
        position: relative;
      }
      img {
        width: 300px;
        margin-top: 50px;
      }
      button {
        width: 30px;
        height: 30px;
        font-size: 20px;
        opacity: 0.1;
        background-color: chartreuse;
        position: absolute;
        top: 50%;
        cursor: pointer;
      }
      button:first-of-type {
        left: 0;
      }
      button:last-child {
        right: 0;
      }
    </style>
  </head>
  <body>
    <div id="auto">
      <p>1/5</p>
      <div>
        <img src="./img/01.jpg" alt="" />
        <button><</button>
        <button>></button>
      </div>
    </div>
    <script src="../js/tools.js"></script>
    <script>
      var img = $("img"),
        box = $("#auto");
      (previous = $("button:nth-of-type(1)")),
        (next = $("button:nth-of-type(2)"));
      p = $("p");
      arrpic = [
        "img/01.jpg",
        "img/02.jpg",
        "img/03.jpg",
        "img/04.jpg",
        "img/05.jpg",
      ];

      var index = 0;
      // var timer =null;
      function Time() {
          timer = setInterval(function () {
          index++;
          if (index == arrpic.length) {
            index = 0;
          }
          p.innerHTML = `${index + 1}/${arrpic.length}`;
          img.src = arrpic[index];
        }, 1000);
      }

      Time();
      box.onmouseout = function () {
        Time();
      };

      box.onmouseover = function () {
        clearInterval(timer);
      };

      previous.onclick = function () {
        index--;
        if (index < 0) {
          index = arrpic.length - 1;
        }
        p.innerHTML =`${index + 1}/${arrpic.length}`;
        img.src = arrpic[index];
      };

      next.onclick = function () {
        index++;
        if (index == arrpic.length) {
          index = 0;
        }
        p.innerHTML =`${index + 1}/${arrpic.length}`;
        img.src = arrpic[index];
      };
    </script>
  </body>
</html>
